<template>
  <div class="good_type">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="clickOperationButton('create')"
          >添加分类</el-button
        >
      </div>
      <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="cat_id"
        border
        lazy
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        :header-cell-style="{ textAlign: 'center' }"
        max-height="500px"
      >
        <el-table-column label="#" width="80" type="index" align="center">
        </el-table-column>

        <el-table-column prop="cat_name" label="分类名称" width="250">
        </el-table-column>
        <el-table-column
          prop="cat_deleted"
          label="是否有效"
          sortable
          width="120"
          align="center"
        >
          <template v-slot="{ row }">
            <el-tag
              :type="row.cat_deleted ? 'danger' : 'success'"
              effect="dark"
            >
              {{ row.cat_deleted ? '无效' : '有效' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="cat_level"
          sortable
          width="150"
          label="分类当前层级"
          align="center"
        >
          <template v-slot="{ row }">
            <el-tag v-if="row.cat_level === 0">
              一级
            </el-tag>
            <el-tag v-else-if="row.cat_level === 1" type="success">
              二级
            </el-tag>
            <el-tag v-else-if="row.cat_level === 2" type="warning">
              三级
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="cat_pid"
          label="分类父 ID"
          sortable
          width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="cat_id"
          label="分类 ID"
          sortable
          width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="{ row }">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="clickOperationButton('edit', row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="clickOperationButton('del', row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        v-if="total > 0"
        :total="total"
        :page.sync="pagenum"
        :limit.sync="pagesize"
        @pagination="getGoodCategories"
      ></Pagination>
    </el-card>
    <!-- Dialog -->
    <el-dialog
      title="添加分类"
      :visible.sync="dialogVisible"
      width="50%"
      :append-to-body="true"
    >
      <el-form
        :model="ruleDialogForm"
        :rules="DialogFormRules"
        ref="ruleDialog"
        label-width="100px"
      >
        <el-form-item label="父级名称" v-if="operationType === 'create'">
          <el-cascader
            style="width:300px;"
            v-model="cascaderValue"
            :options="optionsList"
            :props="props"
            clearable
            placeholder="请选择需要添加商品的分类"
            change-on-select
          ></el-cascader>
        </el-form-item>

        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="ruleDialogForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clickOperationClose">取 消</el-button>
        <el-button type="primary" @click="clickOperationSubmit"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getGoodCategories,
  addCategoriesItem,
  deleteCategoriesItem,
  updateCategories
} from '@/api/goods'
import Pagination from '@/components/Pagination'
export default {
  name: 'GoodType',
  components: { Pagination },
  data() {
    return {
      type: [],
      pagenum: 1,
      pagesize: 10,
      tableData: [],
      total: 0,
      dialogVisible: false,
      ruleDialogForm: {
        cat_name: ''
      },
      cascaderValue: [],
      optionsList: [],
      props: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      operationType: '',
      DialogFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async clickOperationButton(type, item) {
      if (type === 'del') {
        this.$confirm('该操作将悠久删除该数据,是否继续', '删除提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteCategoriesItem(item.cat_id).then(() => {
            this.getGoodCategories()
          })
        })
      } else {
        this.operationType = type
        const { data: res } = await getGoodCategories()
        this.optionsList = res
        this.dialogVisible = true
        if (type === 'edit') {
          this.ruleDialogForm = item
        }
      }
    },
    clickOperationClose() {
      this.operationType = ''
      this.dialogVisible = false
    },
    clickOperationSubmit() {
      this.$refs.ruleDialog.validate(valid => {
        if (!valid) {
          return this.$message.error('请输入分类名称')
        }
        if (this.operationType === 'create') {
          const cascaderValue = this.cascaderValue
          let cat_pid = 0
          let cat_level = 0
          if (cascaderValue.length === 1) {
            cat_pid = cascaderValue[0]
            cat_level = 1
          } else if (cascaderValue.length === 2 || cascaderValue.length === 3) {
            cat_pid = cascaderValue[1]
            cat_level = 2
          }
          addCategoriesItem({
            cat_pid,
            cat_name: this.ruleDialogForm.cat_name,
            cat_level
          }).then(() => {
            this.getGoodCategories()
            this.clickOperationClose()
          })
        } else if (this.operationType === 'edit') {
          updateCategories({
            id: this.ruleDialogForm.cat_id,
            data: {
              cat_name: this.ruleDialogForm.cat_name
            }
          }).then(() => {
            this.clickOperationClose()
          })
        }
      })
    },
    async getGoodCategories() {
      const { data: res } = await getGoodCategories({
        pagenum: this.pagenum,
        pagesize: this.pagesize
      })
      this.tableData = res.result
      this.total = res.total
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getGoodCategories()
  }
}
</script>
<style lang="scss" scoped></style>
